﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class AddScholarShip : System.Web.UI.Page
    {
        MemberAL memberAL = new MemberAL();
        public Guid SelectedMember
        {
            get
            {
                if (Session["MemberId"] == null)
                {
                    return memberAL.GetAllMembers()[0].MemberId;
                }
                else
                {
                    return new Guid(Session["MemberId"].ToString());
                }
            }
            set
            {
                Session["MemberId"] = value;
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

            List<MemberDTO> memberDTO = memberAL.GetAllMembers();
            ddlScholarshipOfferedBy.DataSource = memberDTO;
            ddlScholarshipOfferedBy.DataTextField = "FullName";
            ddlScholarshipOfferedBy.DataValueField = "MemberId";
            ddlScholarshipOfferedBy.SelectedValue = SelectedMember.ToString();
            ddlScholarshipOfferedBy.DataBind();

        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnScholarshipSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                FundAL fundAL = new FundAL();
                decimal capital = 0;
                decimal amountPerSlot = 0;

                if (txtScholarCapital.Text == "")
                {
                    capital = 0;
                }
                else
                {

                    capital = Convert.ToDecimal(txtScholarCapital.Text);
                }

                if (txtScholarshipValue.Text == "")
                {
                    amountPerSlot = 0;
                }
                else
                {

                    amountPerSlot = Convert.ToDecimal(txtScholarshipValue.Text);
                }
                
                ScholarshipDTO scholarshipDTO = new ScholarshipDTO
                {
                    ScholarshipId = Guid.NewGuid(),
                    Capital = capital,
                    Value = amountPerSlot,
                    Title = txtScholarshipTitle.Text,
                    Description = txtScholrshipDescription.Text,
                    OfferedBy = new Guid(ddlScholarshipOfferedBy.SelectedValue),
                    Condition = txtScholrshipCondition.Text,
                    InMemorialOf = txtScholrshipInMemorialOf.Text,
                    
                    //ModifiedBy="Tharsan",
                    ModifiedTime = System.DateTime.Now
                };

                string result = fundAL.SaveScholarShip(scholarshipDTO);
                if (result == "Saved")
                {
                    lblAddScholarship.Text = "The Scholarship Details added Successfully";
                    txtScholarshipTitle.Text = "";
                    txtScholarshipValue.Text = "";
                    txtScholarCapital.Text = "";
                    //txtScholarshipOfferedBy.Text = "";
                    txtScholrshipInMemorialOf.Text = "";
                    txtScholrshipDescription.Text = "";
                    txtScholrshipCondition.Text = "";
                   

                }
                else if (result == "Updated")
                {
                    lblAddScholarship.Text = "The Scholarship Details Updated Successfully";
                    txtScholarshipTitle.Text = "";
                    txtScholarshipValue.Text = "";
                    txtScholarCapital.Text = "";
                    //txtScholarshipOfferedBy.Text = "";
                    txtScholrshipInMemorialOf.Text = "";
                    txtScholrshipDescription.Text = "";
                    txtScholrshipCondition.Text = "";
                }
                else if (result == "AlreadyExists")
                {
                    lblAddScholarship.Text = "The Scholarship  Name is already exists ";
                    txtScholarshipTitle.Text = "";
                    txtScholarshipValue.Text = "";
                    txtScholarCapital.Text = "";
                    //txtScholarshipOfferedBy.Text = "";
                    txtScholrshipInMemorialOf.Text = "";
                    txtScholrshipDescription.Text = "";
                    txtScholrshipCondition.Text = "";
                }
                else
                {
                    lblAddScholarship.Text = "Your data cannot be added";
                    txtScholarshipTitle.Text = "";
                    txtScholarshipValue.Text = "";
                    txtScholarCapital.Text = "";
                    //txtScholarshipOfferedBy.Text = "";
                    txtScholrshipInMemorialOf.Text = "";
                    txtScholrshipDescription.Text = "";
                    txtScholrshipCondition.Text = "";
                }
            }
            catch (Exception ee)
            {
                lblAddScholarship.Text = "There is an exception occured while inserting the Scholarship details " + ee.ToString();
                txtScholarshipTitle.Text = "";
                txtScholarshipValue.Text = "";
                txtScholarCapital.Text = "";
                //txtScholarshipOfferedBy.Text = "";
                txtScholrshipInMemorialOf.Text = "";
                txtScholrshipDescription.Text = "";
                txtScholrshipCondition.Text = "";
            }

        }
    }
}
